/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cjava.system.dao.hibernate;

import java.util.List;
import org.cjava.system.dao.interfac.IBusinessAssociateDao;
import org.cjava.system.dao.util.GenericEntityDao;
import org.cjava.system.model.BusinessAssociate;
import org.cjava.system.model.Person;
import org.cjava.system.model.PersonProfile;
import org.hibernate.Hibernate;
import org.hibernate.transform.Transformers;

/**
 *
 * @author user
 */
public class BusinessAssociateDao extends GenericEntityDao<BusinessAssociate, Long> implements IBusinessAssociateDao {

    @Override
    public List listSQL(String SQL, boolean transform) throws Exception {
        List list;
        if (transform) {
            list = getSessionFactory().getCurrentSession()
                    .createSQLQuery(SQL)
                    .setResultTransformer(Transformers.aliasToBean(BusinessAssociate.class))
                    .list();
        } else {
            list = getSessionFactory().getCurrentSession()
                    .createSQLQuery(SQL)
                    .list();
        }

        return list;
    }

    @Override
    public List listHQL(String hql, int lazyChargeOption, Object object) throws Exception {
        List list = null;
        if (lazyChargeOption == 1) {
            list = getSessionFactory().getCurrentSession()
                    .createSQLQuery(hql)
                    .addEntity(Person.class)
                    .addEntity(BusinessAssociate.class)
                    .list();
        }
        if (lazyChargeOption == 2) {
            list = getSessionFactory().getCurrentSession()
                    .createQuery(hql)
                    .list();
            List<BusinessAssociate> listBus = (List<BusinessAssociate>) list;
            for (BusinessAssociate bu : listBus) {
                for (PersonProfile per : bu.getIdPerson().getPersonProfileList()) {
                    Hibernate.initialize(per);
                }
            }
            list = listBus;
        }

        return list;

    }

}
